// packageF/tourAround/tourAroundStoreList/tourAroundStoreList.js
import util from "../../../utils/util";
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    locationStatus: false, //定位开关
    goodsId: null,
    storeName: "",
    longitude: "", //经纬度
    latitude: "",
    timer: "",
    storeListByGoods: [],
    imgPath: app.globalData.imgPath,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      goodsId: Number(options.goodsId),
      longitude: options.longitude,
      latitude: options.latitude,
    })
    this.getStoreListByGoods();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  navigation(event) {
    let {latitude, longitude, name} = event.currentTarget.dataset;
    let point = app.BMapTransqqMap(parseFloat(longitude), parseFloat(latitude));
    wx.openLocation({
      latitude: point.lat,
      longitude: point.lng,
      scale: 18,
      name: name
    });
  },
  searchChange({ detail }) {
    this.setData({ 
      storeName: detail,
    });
    // 防抖
    if (this.timer) clearTimeout(this.timer);
    this.timer = setTimeout(() => {
      util.debounce(this.getStoreListByGoods());
    }, 500);
  },
  callStore(event) {
    let phone = event.currentTarget.dataset.phone
    wx.makePhoneCall({
      phoneNumber: phone
    });
  },
  getStoreListByGoods() {
    let urlStr = app.getNetAddresss("plugin.travel-around.frontend.goods.goods.getShopAddressByGoods");
    let json = {
      goods_id:this.data.goodsId,
      store_name: this.data.storeName,
      longitude: this.data.longitude,
      latitude: this.data.latitude
    };
    app._postNetWork({
      url: urlStr,
      data:json,
      success:  (resdata) => {
        let res = resdata.data;
        console.log(res);
        if (res.result == 1) {
          this.setData({
            storeListByGoods: res.data,
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})